/**
 * 
 */
package org.jheuristics.ga.operators.rates;

import java.io.Serializable;

import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.operators.PopulationChooser;
import org.jheuristics.ga.operators.Rate;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public class ProportionalRate implements Rate, Serializable {
	private double rate;
	private PopulationChooser chooser;

	/**
	 * TODO
	 *
	 * @param rate
	 * @param chooser
	 */
	public ProportionalRate(double rate, PopulationChooser chooser) {
		if (0 > rate || 1 < rate) {
			throw new IllegalArgumentException();
		}
		this.setPopulationChooser(chooser);
		this.rate = rate;
	}
	
	/**
	 * TODO
	 *
	 * @param chooser
	 */
	public void setPopulationChooser(PopulationChooser chooser) {
		if (null == chooser) {
			throw new NullPointerException();
		}
		this.chooser = chooser;
	}
	
	/**
	 * TODO
	 *
	 * @param status
	 * @param config
	 * @return
	 * @see org.jheuristics.ga.operators.Rate#rate(org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public int rate( GAStatus status, GAConfig config ) {
		return (int)Math.ceil(this.chooser.choose(status, config).size()*rate);
	}

}
